package com.example.ucsur;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ThumbActivity extends Activity {
	
	//Setup string arrays for names and values.
	String fieldValues[] = new String[200];
	String fieldNames[] = new String[200];
	String dataFromActivity;
	
	//Set up textviews.
	TextView[] fieldTextView = new TextView[200];
    JSONObject JSONFromActivity = null;
    
    //Set up counter variables.
	int textViewCounter = 0;
	int nameCounter = 1;
    int valuesCounter = 0;
    
    
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Context mContext = ThumbActivity.this;
		setContentView(R.layout.activity_thumb);
		Bundle extra = getIntent().getExtras();
		dataFromActivity = new String(extra.getString("dataFromActivity"));
		try {
			JSONFromActivity = new JSONObject(dataFromActivity);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(mContext , e.getMessage(), Toast.LENGTH_LONG).show();
		}
		drawFields();
	}
	
	
	public void drawFields()
	{
		TableLayout nm = (TableLayout) findViewById(R.id.relativeLayout);
	    LayoutParams nn = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		fieldNames = JSONFromActivity.names().toString().split(",");
		for (int i3 = 1; i3 < fieldNames.length; i3++)
		{
			fieldNames[nameCounter] = fieldNames[nameCounter].replace("\"", " ").trim();
			nameCounter++;
		}
		fieldNames[0] = fieldNames[0].replace("[", " ").replace("\"", " ").trim();
		for (int i1 = 0; i1 < fieldNames.length; i1++)
		{
			try {
				fieldValues[valuesCounter] = JSONFromActivity.getString(fieldNames[valuesCounter]);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			valuesCounter++; 
		//sup.setText(JSONFromActivity.names().toString());
		}
		for (int i2 = 0; i2 < fieldNames.length; i2++)
		{
			TextView textview = new TextView(this);
			fieldTextView[textViewCounter] = textview;
			if (fieldNames[textViewCounter].equals("mapblolot") || fieldNames[textViewCounter].equals("PropertyOwner2") || fieldNames[textViewCounter].equals("PropertyAddressFull") || fieldNames[textViewCounter].equals("PropertyZip") || fieldNames[textViewCounter].equals("UseCode") || fieldNames[textViewCounter].equals("TaxBillFullAddress2") || fieldNames[textViewCounter].equals("SaleDate1") || fieldNames[textViewCounter].equals("SalePrice1") || fieldNames[textViewCounter].equals("MunDesc") || fieldNames[textViewCounter].equals("SaleDate2") || fieldNames[textViewCounter].equals("SalePrice2"))
			{
				fieldTextView[textViewCounter].setText(fieldNames[textViewCounter] + ": " + fieldValues[textViewCounter]);
				fieldTextView[textViewCounter].setId(textViewCounter);
				nm.addView(fieldTextView[textViewCounter], nn);
				nn.addRule(RelativeLayout.BELOW, fieldTextView[textViewCounter].getId());
			}
			textViewCounter++;
			
			}
		}

	
	
	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}*/
	
}
